package com.john.permission.web;

import com.john.permission.pojo.AjaxResponsePOJO;
import com.john.permission.pojo.Permission;
import com.john.permission.pojo.Role;
import com.john.permission.pojo.User;
import com.john.permission.service.PermissionService;
import com.john.permission.service.RoleService;
import com.john.permission.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
public class MainController {

    @Autowired
    private UserService userService;

    @Autowired
    private RoleService roleService;

    @Autowired
    private PermissionService permissionService;

    @RequestMapping("/main")
    public String login(User user) {
        return "main";
    }

    @ResponseBody
    @GetMapping("/get_permissions")
    public AjaxResponsePOJO getUserPermissions(@SessionAttribute("user") User currentUser) {
        List<Role> roles = roleService.getRoleInfo(currentUser);
        List<Permission> permissionList = permissionService.getUserPermission(roles);
        List<Permission> permissionTree =  permissionService.buildPermissionTree(permissionList);
        return new AjaxResponsePOJO.Builder().ok(permissionTree);
    }

}
